package main

import (
	"context"
	"fmt"
	"log"
	"os"

	pb "gclient/demo"

	"google.golang.org/grpc"
)

const (
	address     = "localhost:9370"
	defaultName = "Hello GRPC"
)

func main() {
	// Contact the server and print out its response.
	name := defaultName
	endpoint := address
	if len(os.Args) > 1 {
		endpoint = os.Args[1]
		fmt.Println("Custom Endpoint Detected!")
	}

	// Set up a connection to the server.
	conn, err := grpc.Dial(endpoint, grpc.WithInsecure())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := pb.NewGreeterClient(conn)

	fmt.Println("Connecting To " + endpoint)
	r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	log.Printf("####### get server Hello response: %s", r.Message)

	r1, err := c.SayHi(context.Background(), &pb.HiRequest{X1: 12, X2: 20})
	if err != nil {
		log.Fatalf("could not greet: %v", err)
	}
	log.Printf("####### get server Hi response: %d", r1.Y)
}
